import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns


# 直方图 & 核密度图（KDE）
def plt_hist():
    """
    分析连续特征的分布、偏度、峰度
    :return:
    """
    feature_data = pd.read_csv('../../data/raw/train.csv')
    features = ['Age', 'DistanceFromHome', 'MonthlyIncome', 'TotalWorkingYears', 'YearsAtCompany']
    for feature0 in features:
        sns.histplot(
            data=feature_data.query('Attrition == 0'),# 0为不离职， 1为离职
            x=feature0,
            kde=True
        )
        plt.title(f'Histogram of {feature0}')
        plt.savefig(f'../../data/fig/plt_hist/{feature0}_0.png')
        plt.show()
    for feature1 in features:
        sns.histplot(
            data=feature_data.query('Attrition == 1'),# 0为不离职， 1为离职
            x=feature1,
            kde=True
        )
        plt.title(f'Histogram of {feature1}')
        plt.savefig(f'../../data/fig/plt_hist/{feature1}_1.png')
        plt.show()

# 箱线图（Box Plot）
def plt_box():
    """
    检测异常值、分布范围和中位数
    点：异常值
    箱中线：
    中位数中位数居中 → 分布较对称
    中位数偏上 → 数据左偏（大部分值较小，少数值很大）
    中位数偏下 → 数据右偏（大部分值较大，少数值很小）
    :return:
    """
    feature_data = pd.read_csv('../../data/raw/train.csv')
    features = ['Age', 'DistanceFromHome', 'MonthlyIncome', 'TotalWorkingYears', 'YearsAtCompany']
    for feature in features:
        sns.boxplot(
            data=feature_data,
            x=feature
        )
        plt.title(f'Box of {feature}')
        plt.savefig(f'../../data/fig/plt_box/{feature}.png')
        plt.show()


# 计数图
def plt_count():
    """
    计算分类特征每类数量
    :return:
    """
    feature_data = pd.read_csv('../../data/raw/train.csv')
    features = ['BusinessTravel', 'Department',
                'Education', 'EducationField',
                'EnvironmentSatisfaction', 'Gender', 'JobInvolvement', 'JobLevel',
                'JobRole', 'JobSatisfaction', 'MaritalStatus',
                'NumCompaniesWorked', 'Over18', 'OverTime', 'PercentSalaryHike',
                'PerformanceRating', 'RelationshipSatisfaction', 'StandardHours',
                'StockOptionLevel', 'TrainingTimesLastYear',
                'WorkLifeBalance', 'YearsInCurrentRole',
                'YearsSinceLastPromotion', 'YearsWithCurrManager']
    for feature0 in features:
        sns.countplot(
            data=feature_data.query('Attrition == 0'),
            x=feature0
        )
        plt.title(f'Box of {feature0}')
        plt.savefig(f'../../data/fig/plt_count/{feature0}_0.png')
        plt.show()
    for feature1 in features:
        sns.countplot(
            data=feature_data.query('Attrition == 1'),
            x=feature1
        )
        plt.title(f'Box of {feature1}')
        plt.savefig(f'../../data/fig/plt_count/{feature1}_1.png')
        plt.show()


if __name__ == '__main__':
    # plt_hist()
    plt_box()
    plt_count()
